import facade
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curPath, "db.ini")


class DeleteHave(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile,
                                             "db",
                                             logger=self.logger)
        self.sets1 = set()
        self.sets2 = set()

    def sleect(self):
        sql = "SELECT isbn13,isbn10 FROM `isbn_cover`.`isbncover` WHERE `url` <> '-1' AND `coverstat` = '1'"
        rows = self.mysqlutils.SelectFromDB(sql)
        for row in rows:
            self.sets1.add(row[0].lower())
            self.sets1.add(row[1].lower())

    def readfile(self):
        for line in BaseDir.read_file_r_mode_yield("./book_cover_20190212.txt"):
            self.sets2.add(line.split("★")[0])

    def Difference(self):
        self.sets3 = self.sets1 - self.sets2
        with open("./difference.txt", 'w', encoding="utf-8") as f:
            for value in self.sets3:
                f.write(value + "\n")

    def set_stat(self):
        ListSql1 = set()
        ListSql2 = set()
        for fline in BaseDir.read_file_r_mode_yield("./notfond.txt"):
            if fline:
                fline = fline.strip()
                if len(fline) == 10:
                    ListSql1.add(fline)
                    #sql = "update `isbn_cover`.`isbncover` set coverstat=0 where isbn10 = '{}'".format(fline)
                else:
                    ListSql2.add(fline)
                    #sql = "update `isbn_cover`.`isbncover` set coverstat=0 where isbn13 = '{}'".format(fline)


        sql = "update `isbn_cover`.`isbncover` set coverstat=0 where isbn10 in {}".format(tuple(ListSql1))
        self.mysqlutils.ExeSqlToDB(sql)
        sql = "update `isbn_cover`.`isbncover` set coverstat=0 where isbn13 in {}".format(tuple(ListSql2))
        self.mysqlutils.ExeSqlToDB(sql)


if __name__ == "__main__":
    delete = DeleteHave()
    # delete.sleect()
    # delete.readfile()
    # delete.Difference()
    delete.set_stat()
